package com.lucker.common.util;



import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by PC on 2019/7/2.
 * 初始化所有遗漏数据的号码
 */
public class InitMissNumberUtil {
    //1-m中选n个号码的所有组合
    public static List<MissCode> getCombinationNumberList(int m, int n) {
    	List<MissCode> missCodeList = new ArrayList<MissCode>();
        List<List<Integer>> reList = _getCombinationNumberList(m,n);
        MissCode missCode;
        for(List<Integer> numberList : reList) {
        	missCode = new MissCode();
        	missCode.setFirstCode(numberList);
        	missCodeList.add(missCode);
        }
        return missCodeList;
    }

    //1-a中选m胆n拖得所有情况
    public static List<MissCode> dantuoCombinationNumber(int a, int m, int n){
    	List<MissCode> retList = new ArrayList<MissCode>();
    	MissCode missCode = null ;
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i< a; i++){
            list.add(i+1);
        }
        List<List<Integer>> danList = combinations(new ArrayList<Integer>(),list,m);//胆码的组合
        for (List<Integer> dan:danList){
            ArrayList<Integer> tempList = new ArrayList<>();//存放拖码的数字
            for (Integer x:list){
                if(dan.contains(x)){

                }else {
                    tempList.add(x);
                }
            }
            //计算拖码的组合
            List<List<Integer>> tuoList = combinations(new ArrayList<Integer>(),tempList,n);//拖码的组合
            
            for (List<Integer> tuo : tuoList){
            	missCode = new MissCode();
                //Log.e("---胆码，存入firstList---",new Gson().toJson(dan));
                //Log.e("---拖码，存入secondList---",new Gson().toJson(tuo));
            	missCode.setFirstCode(dan);
            	missCode.setSecondCode(tuo);
            	retList.add(missCode);
            }
        }
        return retList;
    }

    //1-m中选n个号码的所有排列
    public static List<MissCode> getPermuteList(int m, int n){
    	List<MissCode> retList = new ArrayList<MissCode>();
    	MissCode missCode = null ;
    	
        for (List<Integer> permute : _getCombinationNumberList(m,n)){
        	
            for (List<Integer> last : permute(permute)){
            	missCode = new MissCode();
            	missCode.setFirstCode(last);
            	retList.add(missCode);
                //Log.e("---直选，存入firstList---",new Gson().toJson(last));
            }
        }
        return retList;
    }

    //实现集合的组合

    /**
     * @param selector 选完的集合 初始化为空
     * @param data		待选集合
     * @param n			选出的数量
     */
    public static List<List<Integer>> combinations(List<Integer> selector, List<Integer> data, int n) {
        List<List<Integer>> lists = new ArrayList<>();
        if (n == 0) {
            List<Integer> list = new ArrayList<>();
            for (int i : selector) {
                list.add(i);
            }
            lists.add(list);
            return lists;
        }
        if (data.isEmpty()) {
            return lists;
        }
        //选择第一个元素,将元素放入集合
        selector.add(data.get(0));
        lists.addAll(combinations(selector, data.subList(1, data.size()), n - 1));//从第二个元素开始选择，再选择两个
        // 不选择第一个元素
        selector.remove(selector.size() - 1);
        lists.addAll(combinations(selector, data.subList(1, data.size()), n)); //从第二个元素开始选择，选择两个
        return lists;
    }

    //全排列
    public static List<List<Integer>> permute(List<Integer> nums) {
        List<List<Integer>> all = new ArrayList<List<Integer>>();
        allSort(nums, 0, nums.size() - 1, all);
        return all;
    }

    public static void allSort(List<Integer> array, int begin, int end, List<List<Integer>> all) {
        if (begin == end) {
            List<Integer> origi = new ArrayList<Integer>();
            for (int a : array) {
                origi.add(a);
            }
            all.add(origi);
            return;
        }
        for (int i = begin; i <= end; i++) {
            swap(array, begin, i);
            allSort(array, begin + 1, end, all);
            swap(array, begin, i);
        }
    }

    public static void swap(List<Integer> array, int a, int b) {
        int tem = array.get(a);
        array.set(a, array.get(b));
        array.set(b, tem);
    }
    
    
    //1-m中选n个号码的所有组合
    public static List<List<Integer>> _getCombinationNumberList(int m, int n) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i< m; i++){
            list.add(i+1);
        }
        return combinations(new ArrayList<Integer>(),list,n);
    }
    
    
}
